package com.work.order.controller;

import com.work.entity.Result;
import com.work.entity.StatusCode;
import com.work.order.pojo.Order;
import com.work.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;


    /**
     * 查询全部数据
     *
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll() {
        List<Order> orderList = orderService.findAll();
        return new Result(true, StatusCode.FIND_Ok, "查询成功", orderList);
    }

    /**
     * 生成订单，此功能只有用户能执行
     *
     * @param
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody List<Order> orderList) {
        orderService.add(orderList);
        return new Result(true, StatusCode.ADD_OK, "添加成功");
    }

    /**
     * 修改订单状态，后台操作
     *
     * @return
     */
    @PutMapping("/update")
    public Result update(Long id) {
        orderService.update(id);
        return new Result(true, StatusCode.UPDATE_Ok, "修改成功");
    }

    /**
     * 用户删除订单
     */

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {

        {
            try {
                Integer delete = orderService.delete(id);
                if (delete != null) {
                    return new Result(true, StatusCode.DELETE_OK, "删除设备成功");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return new Result(false, StatusCode.DELETE_ERROR, "删除设备失败");


        }
    }
}
